import adminApi from "@/api/adminApi";
import dateTime from "@/utils/dateutils";
import {upload} from "@/utils/common";

export default {
    name: "index",
    data() {
        return {
            pickerOptions: dateTime.pickerOptions,
            value2: dateTime.value2,
            searchForm: {
                currentPage: 1,
                pageSize: 5,
            },
            tableData: [],
            total: 0,
            editDialog: false,
            formData: {
                adminGender: "1",
                isActive: true,
                adminAvatar: ''
            },
            ids: [],
            roleList: [],
            //表单校验的独享
            formRules: {
                accountName: [
                    {required: true, message: '请输入账户信息', trigger: 'blur'},
                    {pattern: /^[a-zA-Z0-9_-]{4,16}$/, message: '用户由数字字母下划线组成4-16位', trigger: 'blur'},
                ],
                adminName: [
                    {required: true, message: '请输入姓名', trigger: 'blur'},
                ],


                adminEmail: [
                    {required: true, message: '请输入邮箱', trigger: 'blur'},
                    {type: "email", message: '请输入正确的邮箱格式', trigger: 'blur'},
                ],
                adminPhone: [
                    {required: true, message: '请输入手机号码', trigger: 'blur'},
                    {
                        pattern: /^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(18[0,5-9]))\d{8}$/,
                        message: '请输入正确的手机',
                        trigger: 'blur'
                    },
                ],

                adminAddress: [
                    {required: true, message: '地址不能为空', trigger: 'blur'},
                ],

                adminSalary: [
                    {required: true, message: '员工薪资不能为空', trigger: 'blur'},
                ],

                cardCode: [
                    {required: true, message: '身份证号不能为空', trigger: 'blur'},
                    {
                        pattern: /^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/,
                        message: '请输入正确的身份证号',
                        trigger: 'blur'
                    },
                ],
            }
        }
    },
    created() {
        this.searchPage();
        this.getAllRole();
    },

    methods: {
        //////////////////////业务逻辑的方法//////////////////
        /*分页条件查询*/
        async searchPage() {
            let result = await adminApi.searchPage(this.searchForm);
            console.log(result)
            this.tableData = result.data;
            this.total = result.total;
        },

        /**
         * 查询所有的角色
         */

        async getAllRole() {
            this.roleList = await adminApi.getAllRoleList();
        },

        /*添加或者修改品牌*/
        addOrEdit() {
            this.$refs.form.validate(async (valid) => {
                if (valid) {
                    if (this.formData.id) {
                        await adminApi.updateadmin(this.formData);
                    } else {
                        await adminApi.addadmin(this.formData);
                    }
                    this.searchPage();
                    this.editDialog = false;
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });


        },

        /*删除品牌*/
        async batchDelete() {
            await adminApi.batchDelete(this.ids);
            this.searchPage();
        },

        /**通过id查询*/
        async findById(id) {
            this.formData = await adminApi.findById(id);
        },


        //////////////////////处理页面效果////////////////////////////
        chooseTime(value) {
            this.searchForm.startTime = value[0],
                this.searchForm.endTime = value[1]
        },


        /*点击分页效果*/
        pageChangeHandler(page) {
            this.searchForm.currentPage = page;
            this.searchPage();
        },
        /*重置查询表单*/
        resetSearchForm() {
            this.searchForm = {
                currentPage: 1,
                pageSize: 5,
            };

            this.value2 = [];
        },
        /*选择图片*/
        async getImgBaseStr(e) {
            var formData = new FormData();
            formData.append("avatar", e.file);
            this.formData.adminAvatar = await upload(formData);
        },
        /*点击列表中的点击删除*/
        itemBtnClick(row) {
            this.$refs.table.clearSelection();
            this.$refs.table.toggleRowSelection(row)
            this.ids = [];
            this.ids.push(row.id);
        },
        /*勾选框发生改变*/
        selectChange(selection) {
            console.log(selection)
            this.ids = [];
            var map = selection.map(item => item.id);
            map.forEach(item => {
                this.ids.push(item)
            })
        },
        /*点击批量删除*/
        doBatchDelete() {
            this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                //做删除的功能
                this.batchDelete();
            }).catch(() => {
                //取消的功能
                this.$refs.table.clearSelection();
                this.ids = []
            });
        },

        /**
         * 取消删除
         */
        batchDeleteCancel() {
            this.$refs.table.clearSelection();
        }
    }
}
